package cn.foolishbird.hamal.frame.queryParam.handler;

import cn.foolishbird.hamal.frame.queryParam.QueryFieldTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.Map;

/**
 * @author Eric
 * @version 1.0
 * @date 2020-09-29
 */
@Service
public class QueryTypeHandlerManager {

    private static Map<QueryFieldTypeEnum, QueryTypeHandler> handlers = new EnumMap<QueryFieldTypeEnum, QueryTypeHandler>(QueryFieldTypeEnum.class);

    static {
        handlers.put(QueryFieldTypeEnum.INTEGER, new QueryTypeHandler<Integer, String>() {
            @Override
            public Integer convert(String s) {
                if (StringUtils.isBlank(s)) {
                    return null;
                }
                return Integer.valueOf(s);
            }
        });
        handlers.put(QueryFieldTypeEnum.LONG, new QueryTypeHandler<Long, String>() {
            @Override
            public Long convert(String s) {
                if (StringUtils.isBlank(s)) {
                    return null;
                }
                return Long.valueOf(s);
            }
        });
        handlers.put(QueryFieldTypeEnum.DOUBLE, new QueryTypeHandler<Double, String>() {
            @Override
            public Double convert(String s) {
                if (StringUtils.isBlank(s)) {
                    return null;
                }
                return Double.valueOf(s);
            }
        });
        handlers.put(QueryFieldTypeEnum.STRING, new QueryTypeHandler<String, String>() {
            @Override
            public String convert(String s) {
                return s;
            }
        });
        handlers.put(QueryFieldTypeEnum.PICK_DATE_TO_lONG, new PickDateToLongQueryTypeHandler());
    }

    public QueryTypeHandler obtainQueryTypeHandler(QueryFieldTypeEnum queryFieldTypeEnum) {
        if (null == queryFieldTypeEnum) {
            return null;
        }
        return handlers.get(queryFieldTypeEnum);
    }

}
